package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Api(tags = "菜品相关接口")
@RestController
@Slf4j
@RequestMapping("/admin/dish")
public class AdminDishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 清理缓存数据
     *
     * @param pattern
     */
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    /**
     * 新增菜品
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result saveDish(@RequestBody DishDTO dto) {
        log.info("新增菜品：{}", dto);
        dishService.saveDish(dto);
        //清理缓存数据
        String key = "dish_" + dto.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dto) {
        log.info("分页查询:{}", dto);
        PageResult pageResult = dishService.page(dto);
        return Result.success(pageResult);

    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除菜品:{}", ids);
        dishService.deleteByIds(ids);
        //删除所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 回显菜品
     *
     * @param id
     * @return
     */
    @ApiOperation("回显菜品")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        log.info("回显菜品：{}", id);
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     *
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dto) {
        log.info("修改菜品:{]", dto);
        dishService.update(dto);
        //删除所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 启用禁用菜品
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用菜品")
    public Result<String> enableOrDisable(@PathVariable("status") Integer status, Long id) {
        log.info("启用禁用菜品:{}", status);
        dishService.enableOrDisable(status, id);
        //删除所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

}
